﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using CARMSBusiness;
using CARMSBusiness.InterFace;
using CARMSData.EntityClasses;

namespace CARMS.Admin
{
    [Function("Admin.UserEdit")]
    public partial class EditUserInfo : PageBase
    {
       

        private int CurrentUserId
        {
            get
            { 
                return GetIntByParameter("UserId");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDefault();
                LoadUser();
            }
        }

        private void LoadUser()
        {
            var user = SecurityBusiness.GetUserByID(CurrentUserId);
            if (user != null)
            {
                txtLoginName.Enabled = false;
                fpUserInfo.DataSource = user;
                fpUserInfo.DataBind();
                //if (user.EmployeeId.HasValue)
                //{
                //    var employee = DepartmentBusiness.GetEmployeeDataByID(user.EmployeeId.Value);
                //    if (!employee.IsNew)
                //    {
                //        CurrentEmployeeID = user.EmployeeId.Value;
                //        lblRelativeEmployee.Text = employee.FirstName + " " + employee.LastName;
                //        btnRemove.Visible = true;
                //    }
                //}
                radioStatus.SelectedIndex =( user.IsActived ?0:1);
         
                foreach (ListItem item in chkRoles.Items)
                {
                    foreach (var role in user.RoleCollectionViaUserRole)
                    {
                        if (item.Value == role.RoleId.ToString())
                        {
                            item.Selected = true;
                            break;
                        }
                    }
                }
            }
        }

        private void BindDefault()
        {
            ControlHelper.BindRoleList(chkRoles, EmptyType.None);
            ControlHelper.SetRoleToUser(chkRoles, CurrentUserId);
            ControlHelper.BindUserActive(radioStatus);
            if (radioStatus.Items.Count > 0)
            {
                radioStatus.Items[0].Selected = true;
            }

            btnSelectEmployee.OnClientClick = "OpenEmployeeList('" + CurrentUserId + "');return false;";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var data = new UserEntity(CurrentUserId)
                           { 
                               LoginName = txtLoginName.Text.Trim(),
                               FullName = txtFullName.Text.Trim(),
                               Tel = txtPhone.Text.Trim(), 
                               Email = txtEmail.Text.Trim(),
                               Address = txtAddress.Text.Trim(),
                               IsActived = radioStatus.SelectedValue == "1"
                           };
            if(data.IsNew)
            {
                data.CreateDate = DateTime.Now;
            }


            var boolSame = SecurityBusiness.CheckUserLoginNameExist(data);
            if(boolSame)
            {
                base.Alert("The login name already exists in the database,please use another.");
                return;
            }


 
            var selectedRoles = new List<int>();
            foreach (ListItem item in chkRoles.Items)
            {
                if (item.Selected)
                {
                    selectedRoles.Add(int.Parse(item.Value));
                }
            }
            if (data.IsNew)
            {
                data.Password = ApplicationConfiguration.DefaultPassword;
            }
            if (SecurityBusiness.SaveUser(data, selectedRoles))
            {
                Alert("The user have been saved successfully.");
                Response.Redirect(SiteUrls.Admin.UserManage);
            }
            else
            {
                Alert("The user save failed.");
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(SiteUrls.Admin.UserManage);
        }

        protected void btnRefreshEmployee_Click(object sender, EventArgs e)
        {
             
            //var employee = DepartmentBusiness.GetEmployeeDataByID(CurrentEmployeeID.Value);
            //lblRelativeEmployee.Text = employee.FirstName.Trim() + " " + employee.LastName.Trim();
            //txtFullName.Text = lblRelativeEmployee.Text;
            //txtEmail.Text = employee.Email.Trim();
            //txtAddress.Text = employee.Address.Trim();
            //txtMobile.Text = employee.Mobile.Trim();
            //txtPhone.Text = employee.Tel.Trim();
            btnRemove.Visible = true;
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            lblRelativeEmployee.Text = "";
             
            btnRemove.Visible = false;
        }

    }
}
